<% 
  var headContent = {
%>
<!-- //这里包含需要引入的其他css/js文件 -->
<style>
 /*验证：提示信息样式 begin*/
 .am-alert-danger {
    background-color: transparent;
    border-color: transparent;
    color: red;
  }
  .am-alert {
    margin-bottom: 1em;
    padding: .625em;
    background: transparent;
    border: none;
    border-radius: 0;
  }
  /*验证：提示信息样式 end*/
  /*附件上传*/
  .am-progress-xs {
    height: .2rem;
  }
  .am-progress {
    margin-bottom: 0;
  }
</style>
<%};%>
<%layout("/common/_layout.html",{head:headContent}){%>
<div>
  <% include("/admin/waiting_hint.tpl"){}%></div>
<input type="hidden" id="themes-id" name="themes-id" value="${themesId!}" />
<input type="hidden" id="search-type" name="search-type" value="${searchType!}" />
<input type="hidden" id="keywords-org" name="keywordsOrg" value="${keywordsOrg!''}" />
<input type="hidden" id="keywords-city" name="keywordsOrg" value="${keywordsCity!''}" />
<input type="hidden" id="keywords-member" name="keywordsMember" value="${keywordsMember!''}" />
<div class="am-cf am-padding">
  <div class="am-fl am-cf"> <strong class="am-text-primary am-text-lg" onclick="location.href='${base}/themes/list';">系统管理</strong>
    /
    <small>主题模块配置</small>
     /
    <small>${name}</small>
  </div>
</div>
<div class="am-tabs am-margin" data-am-tabs>
  <ul class="am-tabs-nav am-nav am-nav-tabs">
    <li id="li-city"  class="am-active">
      <a href="#tab-city">地市授权</a>
    </li>
    <li id="li-org">
      <a href="#tab-org">门店授权</a>
    </li>
    <li id="li-member">
      <a href="#tab-member">人员授权</a>
    </li>
  </ul>
  <div class="am-tabs-bd">
  
   <div class="am-tab-panel am-fade am-in am-active" id="tab-city">
      <div class="am-g">
        <div class="am-u-sm-12 am-u-md-6" style="padding:0px 1.6rem 1.6rem 1rem;margin:0px;">
          <div class="am-btn-toolbar">
            <div class="am-btn-group am-btn-group-xs">
              <%if(so.hasPermission("THEMES_AUTH_CITY_ADD")) {%>
              <button type="button" class="am-btn am-btn-default" id="add-city-btn">
                <span class="am-icon-plus"></span>
                新增
              </button>
              <%}%>
              <%if(so.hasPermission("THEMES_AUTH_CITY_DEL")) {%>
              <button type="button" id="deleteButtonCity" disabled="disabled" class="am-btn am-btn-default"  onclick="deleteAllCity('${base}/themes/deleteAuthorizationCity')">
                <span class="am-icon-trash-o"></span>
                删除
              </button>
              <%}%></div>
          </div>
        </div>
        <div class="am-u-sm-12 am-u-md-3">
          <div class="am-input-group am-input-group-sm">
            <input type="text" class="am-form-field" id="keyword-city" value="${keywordCity!}" placeholder="按地市名称或渠道编码搜索">
            <span class="am-input-group-btn">
              <button  id="query-city-btn" class="am-btn am-btn-default" type="button">搜索</button>
            </span>
          </div>
        </div>
      </div>
      <div class="am-g">
        <div class="am-u-sm-12">
          <div class="am-scrollable-horizontal">
            <table class="am-table am-table-striped am-table-hover table-main">
              <thead>
                <tr class="am-text-nowrap">
                  <th class="table-check">
                    <input type="checkbox"  id="allCheckCity"/>
                  </th>
                  <th class="table-title">地市</th>
                  <th class="table-title">渠道编码</th>
                </tr>
              </thead>
              <tbody>
                <%if (!isEmpty(cityPage)) {for(list in cityPage.list!){%>
                <tr>
                  <td>
                    <input type="checkbox" name="idsCity" value="${list.id}"></td>
                  <td >${list.DisplayName}</td>
                  <td >${list.NumCode}</td>
                </tr>
                <%}}%></tbody>
            </table>
          </div>
          <div class="am-cf">
            <%if (!isEmpty(cityPage) && cityPage.list.~size >
            0){%>
            <div class="am-fr">
              <% include("/admin/themes_authorization_city_pager.tpl"){}%></div>
            <%}else{%>
            <div class="am-kai">
              <h3>没有找到任何记录!</h3>
            </div>
            <%}%></div>
        </div>
      </div>
    </div>
    
    <div class="am-tab-panel am-fade am-in" id="tab-org">
      <div class="am-g">
        <div class="am-u-sm-12 am-u-md-6" style="padding:0px 1.6rem 1.6rem 1rem;margin:0px;">
          <div class="am-btn-toolbar">
            <div class="am-btn-group am-btn-group-xs">
              <%if(so.hasPermission("THEMES_AUTH_ORG_ADD")) {%>
              <button type="button" class="am-btn am-btn-default" id="add-org-btn">
                <span class="am-icon-plus"></span>
                新增
              </button>
              <%}%>
              <%if(so.hasPermission("THEMES_AUTH_ORG_DEL")) {%>
              <button type="button" id="deleteButton" disabled="disabled" class="am-btn am-btn-default"  onclick="deleteAllOrg('${base}/themes/deleteAuthorizationOrg')">
                <span class="am-icon-trash-o"></span>
                删除
              </button>
              <%}%></div>
          </div>
        </div>
        <div class="am-u-sm-12 am-u-md-3">
          <div class="am-input-group am-input-group-sm">
            <input type="text" class="am-form-field" id="keyword-org" value="${keywordOrg!}" placeholder="按名称或渠道编码搜索">
            <span class="am-input-group-btn">
              <button  id="query-org-btn" class="am-btn am-btn-default" type="button">搜索</button>
            </span>
          </div>
        </div>
      </div>
      <div class="am-g">
        <div class="am-u-sm-12">
          <div class="am-scrollable-horizontal">
            <table class="am-table am-table-striped am-table-hover table-main">
              <thead>
                <tr class="am-text-nowrap">
                  <th class="table-check">
                    <input type="checkbox"  id="allCheck"/>
                  </th>
                  <th class="table-title">门店名称</th>
                  <th class="table-title">渠道编码</th>
                </tr>
              </thead>
              <tbody>
                <%if (!isEmpty(pager)) {for(list in pager.list!){%>
                <tr>
                  <td>
                    <input type="checkbox" name="ids" value="${list.id}"></td>
                  <td >${list.DisplayName}</td>
                  <td >${list.NumCode}</td>
                </tr>
                <%}}%></tbody>
            </table>
          </div>
          <div class="am-cf">
            <%if (!isEmpty(pager) && pager.list.~size >
            0){%>
            <div class="am-fr">
              <% include("/admin/themes_authorization_org_pager.tpl"){}%></div>
            <%}else{%>
            <div class="am-kai">
              <h3>没有找到任何记录!</h3>
            </div>
            <%}%></div>
        </div>
      </div>
    </div>
    
    <div class="am-tab-panel am-fade am-in" id="tab-member">
      <div class="am-g">
        <div class="am-u-sm-12 am-u-md-6" style="padding:0px 1.6rem 1.6rem 1rem;margin:0px;">
          <div class="am-btn-toolbar">
            <div class="am-btn-group am-btn-group-xs">
              <%if(so.hasPermission("THEMES_AUTH_MEM_ADD")) {%>
              <button type="button" class="am-btn am-btn-default" id="add-member-btn">
                <span class="am-icon-plus"></span>
                新增
              </button>
              <%}%>
              <%if(so.hasPermission("THEMES_AUTH_MEM_DEL")) {%>
              <button type="button" id="deleteButtonMember" disabled="disabled" class="am-btn am-btn-default"  onclick="deleteAllMember('${base}/themes/deleteAuthorizationMember')">
                <span class="am-icon-trash-o"></span>
                删除
              </button>
              <%}%>
                <%if(so.hasPermission("THEMES_ALL_MEM_DEL")) {%>
                <button type="button" id="deleteButtonAllMember" class="am-btn am-btn-default"  onclick="deleteAllAuthorizationMember('${base}/themes/deleteAllAuthorizationMember')">
                    <span class="am-icon-trash-o"></span>
                    全部删除
                </button>
                <%}%></div>
          </div>
        </div>
        <div class="am-u-sm-12 am-u-md-3">
          <div class="am-input-group am-input-group-sm">
            <input type="text" class="am-form-field" id="keyword-member" value="${keywordMember!}" placeholder="按姓名\工号\手机号搜索">
            <span class="am-input-group-btn">
              <button  id="query-member-btn" class="am-btn am-btn-default" type="button">搜索</button>
            </span>
          </div>
        </div>
      </div>
      <div class="am-g">
        <div class="am-u-sm-12">
          <div class="am-scrollable-horizontal">
            <table class="am-table am-table-striped am-table-hover table-main">
              <thead>
                <tr class="am-text-nowrap">
                  <th class="table-check">
                    <input type="checkbox"  id="allCheckMember"/>
                  </th>
                  <th class="table-title">姓名</th>
                  <th class="table-title">工号</th>
                  <th class="table-title">手机号</th>
                </tr>
              </thead>
              <tbody>
                <%if (!isEmpty(memberPage)) {for(list in memberPage.list!){%>
                <tr>
                  <td>
                    <input type="checkbox" name="idsMember" value="${list.id}"></td>
                  <td >${list.name}</td>
                  <td >${list.emp_code}</td>
                  <td >${list.mobile}</td>
                </tr>
                <%}}%></tbody>
            </table>
          </div>
          <div class="am-cf">
            <%if (!isEmpty(memberPage) && memberPage.list.~size >
            0){%>
            <div class="am-fr">
              <% include("/admin/themes_authorization_member_pager.tpl"){}%></div>
            <%}else{%>
            <div class="am-kai">
              <h3>没有找到任何记录!</h3>
            </div>
            <%}%></div>
        </div>
      </div>
    </div>
    
    
    
  </div>
</div>
<div class="am-margin">
  <button type="button" class="am-btn am-btn-warning am-btn-xs" onclick="location.href='${base}/themes/authList';">返回上一级</button>
</div>

<!-- The jQuery UI widget factory, can be omitted if jQuery UI is already included -->
<script src="${base}/template/jQuery-File-Upload/js/vendor/jquery.ui.widget.js"></script>
<!-- The Iframe Transport is required for browsers without support for XHR file uploads -->
<script src="${base}/template/jQuery-File-Upload/js/jquery.iframe-transport.js"></script>
<!-- The basic File Upload plugin -->
<script src="${base}/template/jQuery-File-Upload/js/jquery.fileupload.js"></script>
<script type="text/javascript" src="${base}/template/lhgdialog/lhgdialog.min.js"></script>
<script type="text/javascript">
    var urlBase = "${base}";
    var keywordsObj = {};
    $(function() {
      console.log('search-type:'+$("#search-type").val());
      
     if($("#search-type").val() == 'member') {
        $("#tab-org").tabs('open', 2);
     }else if($("#search-type").val() == 'org')
        $("#tab-org").tabs('open', 1);
      else {        
        $("#tab-org").tabs('open', 0);
      }
      
      

      $('#add-org-btn').dialog({
        content: 'url:${base}/organization/dialog', 
        title: '机构选择', 
        lock: true,
        skin: "idialog",
        width: 800,
        height: 550,
        data: function (backdata) {
            if (backdata.length > 0) {
              console.log("id:" + backdata[0].id + ",name:" + backdata[0].name);
              addAuthOrizationOrg(backdata[0].id);              
            }
        }
      });
      
      
      $('#add-city-btn').dialog({
        content: 'url:${base}/organization/cityDialog'+'?themesId='+$("#themes-id").val(), 
        title: '地市选择', 
        lock: true,
        skin: "idialog",
        width: 800,
        height: 550,
        data: function (backdata) {
            addAuthOrizationCity(backdata);              
        }
      });
      
      
      
      
      $('#add-member-btn').dialog({
        content: 'url:${base}/memberOrg/dialog', 
        title: '人员选择', 
        lock: true,
        skin: "idialog",
        width: 800,
        height: 550,
        data: function (backdata) {
            if (backdata.length > 0) {
              console.log("id:" + backdata[0].id + ",name:" + backdata[0].name);
              addAuthOrizationMember(backdata[0].id);              
            }
        }
      });
      
     $("#query-city-btn").on("click",function(){
        console.log('query-city-btn.click');
        if($("#keyword-city").val()) {
          keywordsObj.keywordCity = $("#keyword-city").val();
        }
       
        var keywords = "";
        if(!$.isEmptyObject(keywordsObj)){
          keywords = JSON.stringify(keywordsObj);
        }
        console.log("keywords = "+ keywords);
        $("#keywords-city").val(keywords);
        
        queryCity();
        
      }); 
      
      $("#query-org-btn").on("click",function(){
        console.log('query-org-btn.click');
        if($("#keyword-org").val()) {
          keywordsObj.keywordOrg = $("#keyword-org").val();
        }
       
        var keywords = "";
        if(!$.isEmptyObject(keywordsObj)){
          keywords = JSON.stringify(keywordsObj);
        }
        console.log("keywords = "+ keywords);
        $("#keywords-org").val(keywords);
        queryOrg();
      }); 
        
      $("#query-member-btn").on("click",function(){
        console.log('query-org-btn.click');
        if($("#keyword-member").val()) {
          keywordsObj.keywordMember = $("#keyword-member").val();
        }
       
        var keywords = "";
        if(!$.isEmptyObject(keywordsObj)){
          keywords = JSON.stringify(keywordsObj);
        }
        console.log("keywords = "+ keywords);

        $("#keywords-member").val(keywords);
        queryMember();
      });     


      $("#allCheckMember").click(function() {
          $('input[name="idsMember"]').prop("checked",this.checked);
          if(this.checked){
            $("#deleteButtonMember").prop("disabled", false);
          } else {
            $("#deleteButtonMember").prop("disabled", true);
          }
      });
      
     $("#allCheckCity").click(function() {
          $('input[name="idsCity"]').prop("checked",this.checked);
          if(this.checked){
            $("#deleteButtonCity").prop("disabled", false);
          } else {
            $("#deleteButtonCity").prop("disabled", true);
          }
      });
      
      var $subBox = $("input[name='idsMember']");
      $subBox.click(function(){
        console.log('length:'+$("input[name='idsMember']:checked").length);
        console.log('$subBox.length:' + $subBox.length);
        $("#allCheckMember").prop("checked", $subBox.length == $("input[name='idsMember']:checked").length ? true : false);
        var $idsCheckedMember = $("[name='idsMember']:checked");
        if ($idsCheckedMember.size() > 0) {
          $("#deleteButtonMember").prop("disabled", false);
        } else {
          $("#deleteButtonMember").prop("disabled", true)
        }
      });
      
      var $citySubBox = $("input[name='idsCity']");
      $citySubBox.click(function(){
        console.log('length:'+$("input[name='idsCity']:checked").length);
        console.log('$citySubBox.length:' + $citySubBox.length);
        
        $("#allCheckCity").prop("checked", $citySubBox.length == $("input[name='idsCity']:checked").length ? true : false);
        
        var $idsCheckedCity = $("[name='idsCity']:checked");
        if ($idsCheckedCity.size() > 0) {
          $("#deleteButtonCity").prop("disabled", false);
        } else {
          $("#deleteButtonCity").prop("disabled", true)
        }
      });
      
      
      
      
    });   
    
    function addAuthOrizationOrg(orgid) {
      showWaitingHint("保存配置项，请稍候...");
      console.log('themesId:' + $("#themes-id").val()+",orgid:" + orgid);
      $.ajax({
          url : urlBase + "/themes/addAuthorizationOrg",
          type : 'POST',
          dataType : 'json',
          data : {
              orgID : orgid, themesId : $("#themes-id").val()
          },
          cache : false,
          beforeSend : function(data) {
              // $('#orgID').selected('disable');
              // $("#submit-btn").prop("disabled", true);
          }
      }).done(function (result) {
          console.log('done.result:' + result);
          dismissWaitingHint();
          showDialog("操作成功", "", "success", 2000);
          queryOrg();
      }).fail(function () {
        console.log('fail');
          showDialog("操作失败", "", "error", 2000);
      });
    }
    
   function addAuthOrizationCity(orgids) {
      showWaitingHint("保存配置项，请稍候...");
      
      console.log('themesId:' + $("#themes-id").val()+",orgids:" + orgids);
      
      $.ajax({
          url : urlBase + "/themes/addAuthorizationCity",
          type : 'POST',
          dataType : 'json',
          data :orgids+"&themesId="+$("#themes-id").val(),
          cache : false,
          beforeSend : function(data) {
              // $('#orgID').selected('disable');
              // $("#submit-btn").prop("disabled", true);
          }
      }).done(function (result) {
          console.log('done.result:' + result);
          dismissWaitingHint();
          showDialog("操作成功", "", "success", 2000);
          queryCity();
      }).fail(function () {
        console.log('fail');
          showDialog("操作失败", "", "error", 2000);
          dismissWaitingHint();
      });
    }
    
    function addAuthOrizationMember(memberId) {
      showWaitingHint("保存配置项，请稍候...");
      console.log('themesId:' + $("#themes-id").val()+",memberId:" + memberId);
      $.ajax({
          url : urlBase + "/themes/addAuthorizationMember",
          type : 'POST',
          dataType : 'json',
          data : {
              memberId : memberId, themesId : $("#themes-id").val()
          },
          cache : false,
          beforeSend : function(data) {
              // $('#orgID').selected('disable');
              // $("#submit-btn").prop("disabled", true);
          }
      }).done(function (result) {
          console.log('done.result:' + result);
          dismissWaitingHint();
          showDialog("操作成功", "", "success", 2000);
          queryMember();
      }).fail(function () {
        console.log('fail');
          showDialog("操作失败", "", "error", 2000);
      });
    }
    

    
    
    function showDialog(msgTitle, message, msgType, msgTimer){
      sendMessage({
        title : msgTitle,
        text : message,
        type : msgType,
        timer : msgTimer,
        confirmButtonText : false
      });
    }
    function queryOrg() {
      var url = urlBase + "/themes/authorize?searchType=org&pageNumber=1&" + "themesId=" + $("#themes-id").val();
      if ($("#keywords-org").val().length > 0) {
        url+= "&keywords=" + $("#keywords-org").val();
      }
      document.location.replace(url);
    }
    
   function queryCity() {
      var url = urlBase + "/themes/authorize?searchType=city&pageNumber=1&" + "themesId=" + $("#themes-id").val();
      if ($("#keywords-city").val().length > 0) {
        url+= "&keywords=" + $("#keywords-city").val();
      }
      document.location.replace(url);
    }

    function queryMember() {
      console.log("keywordsMember=" + $("#keywords-member").val());
      var url = urlBase + "/themes/authorize?searchType=member&pageNumber=1&" + "themesId=" + $("#themes-id").val();
      if ($("#keywords-member").val().length > 0) {
        url+= "&keywords=" + $("#keywords-member").val();
      }
      document.location.replace(url);
    }
    // 批量删除
    function deleteAllMember(url) {
      var $idsCheckedCheck = $("[name='idsMember']:checked");
      var $deleteButton = $("#deleteButtonMember");// 删除按钮
      var ids = $idsCheckedCheck.serialize();
      if (confirm('您确定要删除吗？') == true) {
        $.ajax({
          url : url,
          data : ids,
          dataType : "json",
          async : false,
          beforeSend : function(data) {
            $deleteButton.prop("disabled", true)
          },
          success : function(data) {
            $deleteButton.prop("disabled", false)
            if (data.status == "success") {
              $idsCheckedCheck.parent().parent().remove();
            }
            sendMessage({
              title : "删除!",
              text : data.message,
              type : data.status,
              timer : 2000,
              showConfirmButton : false
            });
            queryMember();
          }
        });
      }
    }
    // 全部删除
    function deleteAllAuthorizationMember(url) {
        var $deleteButton = $("#deleteButtonAllMember");// 删除按钮
        if (confirm('您确定要全部删除吗？') == true) {
            $.ajax({
                url : url+'?themesId='+$("#themes-id").val(),
                dataType : "json",
                async : false,
                beforeSend : function(data) {
                    $deleteButton.prop("disabled", true)
                },
                success : function(data) {
                    $deleteButton.prop("disabled", false)
                    sendMessage({
                        title : "删除全部人员!",
                        text : data.message,
                        type : data.status,
                        timer : 2000,
                        showConfirmButton : false
                    });
                    queryMember();
                }
            });
        }
    }

    function deleteAllOrg(url) {
      var $idsCheckedCheck = $("[name='ids']:checked");
      var $deleteButton = $("#deleteButton");// 删除按钮
      var ids = $idsCheckedCheck.serialize();
      if (confirm('您确定要删除吗？') == true) {
        $.ajax({
          url : url,
          data : ids,
          dataType : "json",
          async : false,
          beforeSend : function(data) {
            $deleteButton.prop("disabled", true)
          },
          success : function(data) {
            $deleteButton.prop("disabled", false)
            if (data.status == "success") {
              $idsCheckedCheck.parent().parent().remove();
            }
            sendMessage({
              title : "删除!",
              text : data.message,
              type : data.status,
              timer : 2000,
              showConfirmButton : false
            });
            queryOrg();
          }
        });
      }
    }
    
     function deleteAllCity(url) {
      var $idsCheckedCheck = $("[name='idsCity']:checked");
      var $deleteButton = $("#deleteButtonCity");// 删除按钮
      var ids = $idsCheckedCheck.serialize();
      if (confirm('您确定要删除吗？') == true) {
        $.ajax({
          url : url,
          data : ids,
          dataType : "json",
          async : false,
          beforeSend : function(data) {
            $deleteButton.prop("disabled", true)
          },
          success : function(data) {
            $deleteButton.prop("disabled", false)
            if (data.status == "success") {
              $idsCheckedCheck.parent().parent().remove();
            }
            sendMessage({
              title : "删除!",
              text : data.message,
              type : data.status,
              timer : 2000,
              showConfirmButton : false
            });
            queryCity();
          }
        });
      }
    }
    
  </script>
<%}%>